package com.jywl.util;

import java.util.List;

import org.springframework.web.context.ContextLoader;

import com.jywl.entity.Column;
import com.jywl.entity.SingleConfig;
import com.jywl.entity.SingleConfigSet;

public class GeneratorUtil {

	public static String gcFile(SingleConfigSet params,int type){
		
		String temfolder=FileUtil.getTempFolder()+CommUtil.getUUID()+"\\";
		String projectName = params.getSingleConfig().getProjectName();	
		String classname = params.getSingleConfig().getClassname();
		String javaSrc = params.getSingleConfig().getJavaSrc();
		String jspSrc = params.getSingleConfig().getJspSrc();
		String jsSrc = params.getSingleConfig().getJsSrc();
		String excelSrc = params.getSingleConfig().getExcelSrc();
		String packagename = params.getSingleConfig().getPackagename();
		
		String javatemp = temfolder + projectName + "\\"+javaSrc+"\\"+packagename.replace(".", "\\")+"\\";	
		String entityFile=javatemp+"entity\\"+classname+".java";
		String controllerFile=javatemp+"controller\\"+classname+"Controller.java";
		String mapperInterFile=javatemp+"mapper\\"+classname+"Mapper.java";
		String mapperXmlFile=javatemp+"mapper\\"+classname+"Mapper.xml";
		String jspFile= temfolder + projectName + "\\"+jspSrc+"\\"+classname.toLowerCase()+"\\"+classname.toLowerCase()+".jsp";
		String jsFile=temfolder + projectName + "\\"+jsSrc+"\\"+classname.toLowerCase()+"\\"+classname.toLowerCase()+".js";
		String excelFile = temfolder + projectName + "\\"+excelSrc+"\\"+classname.toLowerCase()+".xlsx";

		if(null == params.getSingleConfig().getSearchCount()){
			 params.getSingleConfig().setSearchCount(2);
		}
		params.getSingleConfig().setEntityFile(entityFile);
		params.getSingleConfig().setControllerFile(controllerFile);
		params.getSingleConfig().setMapperInterFile(mapperInterFile);
		params.getSingleConfig().setMapperXmlFile(mapperXmlFile);
		params.getSingleConfig().setJspFile(jspFile);
		params.getSingleConfig().setJsFile(jsFile);
		
		
		
		FileUtil.creatFile(entityFile);
		FileUtil.creatFile(controllerFile);
		FileUtil.creatFile(mapperInterFile);
		FileUtil.creatFile(mapperXmlFile);
		FileUtil.creatFile(jspFile);
		FileUtil.creatFile(jsFile);
		FileUtil.createExcelFile(excelFile,params.getColumns());
		
		
		if(1==type){
			single(params.getSingleConfig(),params.getColumns());
		}else if(2==type){
			searchEntity(params.getSingleConfig(),params.getColumns());
		}
		
		String zipFile=temfolder+classname+".zip";
		FileUtil.createZip(temfolder+projectName, zipFile);
		return zipFile;
	}
	public static void single(SingleConfig config,List<Column> olumns) {	
		
		creatEntityFile(config.getEntityFile(),config.getClassname(),config.getPackagename(),olumns);
		creatControllerFile(config.getControllerFile(),config.getClassname(),config.getPackagename(),config.getTitle(),olumns,1);
		creatMapperInterFile(config.getMapperInterFile(),config.getClassname(),config.getPackagename(),1);
		creatMapperXmlFile(config.getMapperXmlFile(), config.getClassname(), config.getPackagename(), config.getTablename(), olumns);
		creatJspFile(config.getJspFile(),config.getTitle(),config.getClassname(),config.getSearchCount(),config.getProjectName(),olumns,1);
		creatJsFile(config.getJsFile(),config.getClassname(),1);
	}
	
	public static void searchEntity(SingleConfig config,List<Column> olumns) {
		creatEntityFile(config.getEntityFile(),config.getClassname(),config.getPackagename(),olumns);
		creatControllerFile(config.getControllerFile(),config.getClassname(),config.getPackagename(),config.getTitle(),olumns,2);
		creatMapperInterFile(config.getMapperInterFile(),config.getClassname(),config.getPackagename(),2);
		creatMapperXmlFileSearch(config.getMapperXmlFile(), config.getClassname(), config.getPackagename(), config.getSql(), olumns);
		
		
		creatJspFile(config.getJspFile(),config.getTitle(),config.getClassname(),config.getSearchCount(),config.getProjectName(),olumns,2);
		creatJsFile(config.getJsFile(),config.getClassname(),2);
	}
	
	
	
	
	
	
	private static void creatEntityFile(String entityFile,String classname,String packagename,List<Column> columns) {
		String modelpath= ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/resources/model/Entity.j");
		String javaTxt = FileUtil.txt2String(modelpath);
		//${package}. ${classname} ${property} ${setGet} ${toString}
		String property = EntityUtil.getProperty(columns);
		String setGet = EntityUtil.getSetGet(columns);
		String toString = EntityUtil.getToString(columns,classname);
		javaTxt = javaTxt.replace("${package}", packagename)
			.replace("${classname}", classname)
			.replace("${property}", property)
			.replace("${setGet}", setGet)
			.replace("${toString}", toString);
		FileUtil.writeTxtFile(javaTxt,entityFile);
	}
	
	
	private static void creatControllerFile(String controllerFile,String classname,String packagename,String title,List<Column> columns,int type) {
		
		String modelpath= ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/resources/model/Controller.j");
		if(2==type){
			modelpath= ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/resources/model/ControllerSearch.j");
		}
		String javaTxt = FileUtil.txt2String(modelpath);
		javaTxt = javaTxt.replace("${package}", packagename)
				.replace("${classname}", classname)
				.replace("${title}", title)
				.replace("${classnameL}", classname.toLowerCase());
		for (Column column : columns) {
			if(null!=column.getIspk()&&1==column.getIspk()){
				javaTxt = javaTxt.replace("${pro}", captureName(column.getPropertyname()));
			}
		}
		
		//${classnameL} ${classname} ${package}
		FileUtil.writeTxtFile(javaTxt,controllerFile);
 	}

	private static void creatMapperInterFile(String mapperInterFile,String classname,String packagename ,int type) {
		String modelpath= ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/resources/model/Mapper.j");
		if(2==type){
			modelpath= ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/resources/model/MapperSearch.j");
		}	
		String javaTxt = FileUtil.txt2String(modelpath);
		javaTxt = javaTxt.replace("${package}", packagename)
				.replace("${classname}", classname)
				.replace("${classnameL}", classname.toLowerCase());
		FileUtil.writeTxtFile(javaTxt,mapperInterFile);
	}
	
	private static void creatMapperXmlFile(String mapperXmlFile,String classname,String packagename,String tablename, List<Column> columns) {
		//"${package}.mapper.${classname}
		String modelpath= ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/resources/model/Mapper.x");
		String javaTxt = FileUtil.txt2String(modelpath);
		javaTxt = javaTxt.replace("${package}", packagename)
				.replace("${classname}", classname)
				.replace("${tablename}",tablename);
		
		String queryPro = EntityUtil.getQueryPro(columns);
		String queryWhere = EntityUtil.getQueryWhere(columns);
		String queryOrderBy = EntityUtil.getQueryOrderBy(columns);
		String addCol = EntityUtil.getAddCol(columns);
		String addPro = EntityUtil.getAddPro(columns);
		String updateSet = EntityUtil.getUpdateSet (columns);
		String updateSetPk = EntityUtil.getUpdateSetPk(columns);
		String updateWhere = EntityUtil.getUpdateWhere(columns);
		String deletePkCol = EntityUtil.getDeletePkCol(columns);
		String deletePkPro = EntityUtil.getDeletePkPro(columns);
		javaTxt = javaTxt.replace("${queryPro}", queryPro)
				.replace("${queryWhere}", queryWhere)
				.replace("${queryOrderBy}", queryOrderBy)
				.replace("${addCol}", addCol)
				.replace("${addPro}", addPro)
				.replace("${updateSet}", updateSet)
				.replace("${updateSetPk}", updateSetPk)
				.replace("${updateWhere}", updateWhere)
				.replace("${deletePkCol}", deletePkCol)
				.replace("${deletePkPro}", deletePkPro);
		
		FileUtil.writeTxtFile(javaTxt,mapperXmlFile);
	}
	private static void creatMapperXmlFileSearch(String mapperXmlFile, String classname, String packagename, String sql,
			List<Column> columns) {
		
		String modelpath= ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/resources/model/MapperSearch.x");
		String javaTxt = FileUtil.txt2String(modelpath);
		javaTxt = javaTxt.replace("${package}", packagename)
				.replace("${classname}", classname)
				.replace("${sql}",sql);
		String queryWhere = EntityUtil.getQueryWhereSearch(columns);
		String queryOrderBy = EntityUtil.getQueryOrderBy(columns);
	
		javaTxt = javaTxt.replace("${queryWhere}", queryWhere)
				.replace("${queryOrderBy}", queryOrderBy);
			
		
		FileUtil.writeTxtFile(javaTxt,mapperXmlFile);
		
		
		
	}
	private static void creatJspFile(String jspFile,String title,String classname,int searchCount,String projectName,List<Column> columns,int type) {
		
		if(0==searchCount){
			
			searchCount =2;
		}
		String modelpath= ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/resources/model/jsp.j");
		if(2==type){
			 modelpath= ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/resources/model/jspSearch.j");
		}
		String searchTr = EntityUtil.getSearchTr(columns,searchCount);
		String tableth = EntityUtil.getTableth(columns);
		String editTr = EntityUtil.getEditTr(columns);
		String pkPro = EntityUtil.getDeletePkPro(columns);
		
		String javaTxt = FileUtil.txt2String(modelpath);
		javaTxt = javaTxt.replace("${title}", title)
				.replace("${projectName}",projectName)
				.replace("${classnameL}", classname.toLowerCase())
				.replace("${searchTr}",searchTr)
				.replace("${tableth}",tableth)
				.replace("${editTr}",editTr)
				.replace("${pkPro}",pkPro)
				;
			
		
		
		
		FileUtil.writeTxtFile(javaTxt,jspFile);
	}
	private static void creatJsFile(String jsFile,String classname,int type) {
		String modelpath= ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/resources/model/js.j");
		if(2==type){
			 modelpath= ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/resources/model/jsSearch.j");
		}
		String javaTxt = FileUtil.txt2String(modelpath);
		javaTxt = javaTxt.replace("${classnameL}", classname.toLowerCase());
		FileUtil.writeTxtFile(javaTxt,jsFile);
	}




	
	public static String getSpace(int qty){
		String s = "";
		for(int i =1;i<=qty;i++){
			s=s+"    ";
		}
		return s;
	}
	//首字母大写
	public static String captureName(String name) {
        char[] cs=name.toCharArray();
        cs[0]-=32;
        return String.valueOf(cs);
		        
	}

	
}
